import ajax from '../assets/ajax.js'
import Vue from 'vue'

// 获取订单信息
const GET_ORDER_INFO = 'GET_ORDER_INFO'
// 获取订单信息成功
const GET_ORDER_INFO_SUCCESS = 'GET_ORDER_INFO_SUCCESS'
// 获取订单信息失败
const GET_ORDER_INFO_ERROR = 'GET_ORDER_INFO_ERROR'

let state = {
  data: null
};

const mutations = {
  [GET_ORDER_INFO_SUCCESS](state, data) {
    state.data = data
  },
  [GET_ORDER_INFO_ERROR](state) {
    // TODO
  }
};

const actions = {
  [GET_ORDER_INFO]({
    commit,
    state
  }, params) {
    // if (state.data) return
    const data = ajax.handleData({
      methodId: 'J004',
      data: {
        mobile: params.mobile
      }
    })
    return new Promise((resolve, reject) => {
      Vue.http.post(ajax.url, data).then((response) => {
        const code = response.body.code
        if (code === '0') {
          const data = response.body.res.data
          commit('GET_ORDER_INFO_SUCCESS', data)
          resolve()
        }
      }, (error) => {
        commit([GET_ORDER_INFO_ERROR])
        reject()
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
